package br.com.gkarian.mydesk.intercepts;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.resource.ResourceMethod;

@RequestScoped
@Intercepts
public class LoggingInterceptor implements Interceptor {

	private static final Logger LOG = Logger
			.getLogger(LoggingInterceptor.class);

	private HttpServletRequest request;

	private LoggingInterceptor(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public boolean accepts(ResourceMethod method) {
		return true;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void intercept(InterceptorStack stack, ResourceMethod method,
			Object resourceInstance) throws InterceptionException {

		Enumeration enParams = request.getParameterNames();
		while (enParams.hasMoreElements()) {
			String paramName = (String) enParams.nextElement();
			LOG.info(paramName + "=> " + request.getParameter(paramName));
		}

		stack.next(method, resourceInstance);
	}
}
